<?php
/**
 * Created by PhpStorm.
 * User:吴鑫
 */

namespace backend\service;

use common\models\Feed;
use common\service\ConstatService;
use yii\data\Pagination;
use yii\db\Query ;
use Yii ;

class FeedService
{

    /**
     * 问题列表
     * @param $limit
     * @param null $searchData
     * @param null $orderby
     * @return array
     */
    public static function findFeedList($limit,$searchData=null,$orderby=null){
        $pages = null ;
        $query = new Query();

        $query->select('*')
            ->from(Feed::tableName()) ;

        $typeId = Yii::$app->admin->identity->type_id ;
//        if($typeId==ConstatService::ADMIN_USER_TYPE_ID_MANAGER ||$typeId == ConstatService::ADMIN_USER_TYPE_ID_SUPER_ADMIN){
//            //管理员
//        }else{
//            //普通员工
//            $query->andWhere(["deal_user_id"=> Yii::$app->admin->identity->user_id]) ;
//        }


        if(!empty($searchData['status']) && $searchData['status']!=-1){
            $query->andWhere(["static"=>$searchData['status']]) ;
        }

        if(!empty($searchData['verify']) && $searchData['verify']!=-1){
            $query->andWhere(["verify"=>$searchData['verify']]) ;
        }

        if(!empty($searchData['add_time_strart'])){
            $query->andWhere([">=","add_time",$searchData['add_time_strart']]) ;
        }

        if(!empty($searchData['add_time_end'])){
            $query->andWhere(["<","add_time",$searchData['add_time_end']]) ;
        }

        if(!empty($searchData['company_name'])){
            $query->andWhere(["like","company_name",$searchData['company_name']]) ;
        }


        if(!empty($orderby)){
            $query->orderBy($orderby) ;
        }else{
            $query->orderBy("id desc") ;
        }

        if(isset($limit)){
            $pages = new Pagination(['totalCount' =>$query->count(), 'defaultPageSize' => $limit]);
            $query->offset($pages->offset)
                ->limit($limit) ;
        }

        $dataList = $query->all() ;

        $num = 0 ;
        $dealing = 0 ;
        $user_id = Yii::$app->admin->identity->user_id ;
        foreach($dataList as $val){
            if($val['static']==ConstatService::FEED_STATIC_NOT_DEAL){
                $num = $num + 1 ;
            }
            if($val['static']==ConstatService::FEED_STATIC_DEALING && $val['deal_user_id']==$user_id){
                $dealing = $dealing + 1 ;
            }
        }

        return ["list" => $dataList, "page" => $pages ,"not_deal_num"=>$num,"dealing_num"=>$dealing];

    }

    /**
     * 导出
     * @param null $searchData
     * @return array
     */
    public static function exportFeed($searchData=null,$orderby=null){
        $pages = null ;
        $query = new Query();

        $query->select('*')
            ->from(Feed::tableName()) ;

        if(!empty($searchData['status']) && $searchData['status']!=-1){
            $query->andWhere(["static"=>$searchData['status']]) ;
        }

        if(!empty($searchData['verify']) && $searchData['verify']!=-1){
            $query->andWhere(["verify"=>$searchData['verify']]) ;
        }

        if(!empty($searchData['add_time_strart'])){
            $query->andWhere([">=","add_time",$searchData['add_time_strart']]) ;
        }

        if(!empty($searchData['add_time_end'])){
            $query->andWhere(["<","add_time",$searchData['add_time_end']]) ;
        }


        if(!empty($orderby)){
            $query->orderBy($orderby) ;
        }else{
            $query->orderBy("id desc") ;
        }

        $count = $query->count() ;

        return ["query" => $query, "count" => $count];

    }


}